﻿using SuperMap.Data;
using SuperMap.Realspace;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Ycjw.PipeLine.Map.Basic.Conditions;
using Ycjw.PipeLine.Map.Basic.SuperMap;

namespace Ycjw.PipeLine.Map.Tools.Conditions.Statistics
{
    public partial class GXMSStatistics : ConditionPanel
    {
        private LayerWithDataListPanel LayerWithDataListPanel;
        public GXMSStatistics() : base()
        {
            this.ConditionPanelName = "管线按埋深统计";
            InitializeComponent();
        }
        private readonly string GJFIELDNAME = "SSStart_Depth";
        public override object Do(DockPanel dockPanel = null)
        {
            DataTable dt = new DataTable();
            dt.TableName = ConditionPanelName;
            dt.Columns.Add("系列");
            if (uiRadioButton5Segment.Checked || uiRadioButton10Segment.Checked)
            {
                dt = StatisticsBySegment(dt);
            }
            else
            {
                dt = StatisticsByAll(dt);
            }

            return dt;
        }
        private DataTable StatisticsBySegment(DataTable dt)
        {
            List<Layer3DDataset> datasets = this.LayerWithDataListPanel.SelectLayer3DDatasets;
            var rMin = double.MaxValue;
            var rMax = double.MinValue;
            var hasNull = false;
            foreach (Layer3DDataset layer3DDataset in datasets)
            {
                var dataSetVector = layer3DDataset.Dataset as SuperMap.Data.DatasetVector;
                if (dataSetVector.FieldInfos.IndexOf(GJFIELDNAME) < 0) continue;
                DatasetVectorEx dvEx = new DatasetVectorEx(layer3DDataset.Dataset.Name);
                dvEx.DatasetVector = dataSetVector;

                double[] minMax = dvEx.GetMinMaxValue(GJFIELDNAME);
                if (minMax.Length > 1)
                {
                    if (rMin > minMax[0])
                        rMin = minMax[0];
                    if (rMax < minMax[1])
                        rMax = minMax[1];
                }
                if (!hasNull && dvEx.HasNullVal(GJFIELDNAME)) hasNull = true;
            }
            if (rMin == double.MaxValue || rMax == double.MaxValue) return null;

            var levelCount = 5;
            if (uiRadioButton5Segment.Checked) levelCount = 6;
            if (uiRadioButton10Segment.Checked) levelCount = 11;
            if (rMin == rMax) levelCount = 1;
            if (levelCount == 1)
            {
                dt.Columns.Add(rMin + string.Empty);
            }
            else
            {
                var levelStep = (rMax - rMin) / levelCount - 1;
                for (int i = 0; i < levelCount - 1; i++)
                {
                    var v1 = (rMin + (levelStep * (i + 0))).ToString("f2");
                    var v2 = (rMin + levelStep * (i + 1)).ToString("f2");
                    if (i < levelCount - 2)
                        dt.Columns.Add(v1 + "≤X＜" + v2);
                    else
                    {
                        dt.Columns.Add(v1 + "≤X≤" + v2);
                    }
                }
            }
            if (hasNull) dt.Columns.Add("未知");

            foreach (Layer3DDataset layer3DDataset in datasets)
            {
                var dataSetVector = layer3DDataset.Dataset as SuperMap.Data.DatasetVector;
                if (dataSetVector.FieldInfos.IndexOf(GJFIELDNAME) < 0) continue;
                DatasetVectorEx dvEx = new DatasetVectorEx(layer3DDataset.Dataset.Name);
                dvEx.DatasetVector = dataSetVector;
                var dr = dt.NewRow();
                dr[0] = layer3DDataset.Caption;
                if (levelCount == 1)
                {
                    double count = dvEx.StaticsticsLengthByMinMax(GJFIELDNAME, rMin - 1, rMax + 1, true);
                    dr[1] = count;
                }
                else
                {
                    var levelStep = (rMax - rMin) / levelCount - 1;
                    for (int i = 0; i < levelCount - 1; i++)
                    {
                        double count;
                        if (i < levelCount - 2)
                            count = dvEx.StaticsticsLengthByMinMax(GJFIELDNAME, rMin + levelStep * (i + 0), (rMin + levelStep * (i + 1)), false);
                        else
                            count = dvEx.StaticsticsLengthByMinMax(GJFIELDNAME, rMin + levelStep * (i + 0), (rMin + levelStep * (i + 1)), true);
                        dr[i + 1] = count;
                    }
                }
                if (hasNull)
                {
                    dr[dr.ItemArray.Length - 1] = dvEx.NullValCount(GJFIELDNAME);
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }
        public override bool ConditionValid()
        {
            if (this.LayerWithDataListPanel.SelectLayer3DDatasets.Count == 0)
                return false;

            return true;
        }
        private DataTable StatisticsByAll(DataTable dt)
        {
            List<Layer3DDataset> datasets = this.LayerWithDataListPanel.SelectLayer3DDatasets;
            List<Dictionary<string, double>> results = new List<Dictionary<string, double>>();
            foreach (Layer3DDataset layer3DDataset in datasets)
            {
                var dataSetVector = layer3DDataset.Dataset as SuperMap.Data.DatasetVector;
                if (dataSetVector.FieldInfos.IndexOf(GJFIELDNAME) < 0) continue;
                DatasetVectorEx dvEx = new DatasetVectorEx(layer3DDataset.Dataset.Name);
                dvEx.DatasetVector = dataSetVector;
                var result = dvEx.UniqueValLength(GJFIELDNAME);
                results.Add(result);
                foreach (var key in result.Keys)
                {
                    var tmpKeyDouble = 0.0d;
                    double.TryParse(key, out tmpKeyDouble);
                    var tmpKey = tmpKeyDouble.ToString("f2");
                    if (!dt.Columns.Contains(tmpKey))
                        dt.Columns.Add(tmpKey);
                }
            }
            foreach (Layer3DDataset layer3DDataset in datasets)
            {
                var dr = dt.NewRow();
                dr[0] = layer3DDataset.Caption;
                dt.Rows.Add(dr);
            }
            var rowIndex = 0;
            foreach (var r in results)
            {
                var dr = dt.Rows[rowIndex++];
                foreach (var key in r.Keys)
                {
                    var tmpKeyDouble = 0.0d;
                    double.TryParse(key, out tmpKeyDouble);
                    var tmpKey = tmpKeyDouble.ToString("f2");
                    dr[tmpKey] = r[key];
                }
            }
            return dt;
        }
        protected override void OnLoad(EventArgs e)
        {
            LayerWithDataListPanel = new LayerWithDataListPanel()
            {
                Dock = System.Windows.Forms.DockStyle.Fill
            };
            Scene scene = this.Scene;
            if (scene == null) return;
            LayerWithDataListPanel.Init(scene);
            //LayerWithDataListPanel.LoadToVector();
            var types = new List<DatasetType>()
            {
                DatasetType.Line,
                DatasetType.Line3D ,
                 DatasetType.Network,
                DatasetType.Network3D
            };
            LayerWithDataListPanel.LoadTo(types, true);
            this.uiGroupBoxtjtc.Controls.Add(LayerWithDataListPanel);
        }

      
    }
}
